package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberSeekService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.awt.*;

@RestController
@RequestMapping("/member")
@CrossOrigin
public class MemberSeekController {

    @Reference
    private MemberSeekService memberSeekService;

    /**
     * 分页查询+模糊查询会员信息
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        try {
            PageResult pageResult = memberSeekService.pageQuery(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    /**
     * 根据id查询会员
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        try {
            Member member = memberSeekService.findById(id);
            return new Result(true,MessageConstant.QUERY_MEMBER_SUCCESS,member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    /**
     * 编辑会员信息
     * @param member
     * @return
     */
    @PutMapping("/update")
    public Result updateMember(@RequestBody Member member){
        try {
            memberSeekService.update(member);
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    /**
     * 新增会员信息
     * @param member
     * @return
     */
    @PostMapping("/add")
    public Result addMember(@RequestBody Member member){
        try {
            memberSeekService.add(member);
            return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    /**
     * 修改会员状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/{id}/{status}")
    public Result updateMemberStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status){
        try {
            memberSeekService.updateById(id,status);
            return new Result(true,MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_MEMBER_STATUS_FAIL);
        }
    }
}
